{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e3993515-9710-4ac4-89e9-b35ebb81e920",
   "metadata": {
    "id": "rhqRhkElpJ0z"
   },
   "source": [
    "# Transforming\n",
    "\n",
    "In this notebook, we will explore how to use Large Language Models for text transformation tasks such as language translation, spelling and grammar checking, tone adjustment, and format conversion.\n",
    "\n",
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "41df0348",
   "metadata": {
    "height": 131,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv()) # read local .env file\n",
    "\n",
    "openai.api_key  = os.getenv('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a85ee0f",
   "metadata": {
    "height": 148,
    "tags": []
   },
   "outputs": [],
   "source": [
    "def get_completion(prompt, model=\"gpt-3.5-turbo\", temperature=0): \n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        temperature=temperature, \n",
    "    )\n",
    "    return response.choices[0].message[\"content\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d8f0bd8-628e-4c36-bcd0-2110162f25fc",
   "metadata": {
    "id": "zdxC4c6pwqA5"
   },
   "source": [
    "## Translation\n",
    "\n",
    "ChatGPT is trained with sources in many languages. This gives the model the ability to do translation. Here are some examples of how to use this capability."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c4df6ff",
   "metadata": {
    "height": 114,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hola, me gustaría ordenar una licuadora.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Translate the following English text to Spanish: \\ \n",
    "```Hi, I would like to order a blender```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7300ed9b",
   "metadata": {
    "height": 114,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is French.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Tell me which language this is: \n",
    "```Combien coûte le lampadaire?```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "791e789b",
   "metadata": {
    "height": 131,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "French: ```Je veux commander un ballon de basket```\n",
      "\n",
      "Spanish: ```Quiero ordenar un balón de baloncesto```\n",
      "\n",
      "English: ```I want to order a basketball```\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Translate the following  text to French and Spanish\n",
    "and English pirate: \\\n",
    "```I want to order a basketball```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fcf7eb63",
   "metadata": {
    "height": 131,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Formal: ¿Le gustaría ordenar una almohada?\n",
      "Informal: ¿Te gustaría ordenar una almohada?\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Translate the following text to Spanish in both the \\\n",
    "formal and informal forms: \n",
    "'Would you like to order a pillow?'\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8edb56d2-a32a-470f-9f40-4fc5b1ea0849",
   "metadata": {
    "id": "-hN2bczQrRC1"
   },
   "source": [
    "### Universal Translator\n",
    "Imagine you are in charge of IT at a large multinational e-commerce company. Users are messaging you with IT issues in all their native languages. Your staff is from all over the world and speaks only their native languages. You need a universal translator!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68a40bf0",
   "metadata": {
    "height": 131,
    "tags": []
   },
   "outputs": [],
   "source": [
    "user_messages = [\n",
    "  \"La performance du système est plus lente que d'habitude.\",  # System performance is slower than normal         \n",
    "  \"Mi monitor tiene píxeles que no se iluminan.\",              # My monitor has pixels that are not lighting\n",
    "  \"Il mio mouse non funziona\",                                 # My mouse is not working\n",
    "  \"Mój klawisz Ctrl jest zepsuty\",                             # My keyboard has a broken control key\n",
    "  \"我的屏幕在闪烁\"                                               # My screen is flashing\n",
    "] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "552d0db9",
   "metadata": {
    "height": 199,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original message (French): La performance du système est plus lente que d'habitude.\n",
      "English: \"The system performance is slower than usual.\"\n",
      "\n",
      "Korean: \"시스템 성능이 평소보다 느립니다.\" \n",
      "\n",
      "Original message (This is Spanish.): Mi monitor tiene píxeles que no se iluminan.\n",
      "English: \"My monitor has pixels that do not light up.\"\n",
      "\n",
      "Korean: \"내 모니터에는 빛나지 않는 픽셀이 있습니다.\" \n",
      "\n",
      "Original message (Italian): Il mio mouse non funziona\n",
      "English: My mouse is not working\n",
      "Korean: 내 마우스가 작동하지 않습니다 \n",
      "\n",
      "Original message (This is Polish.): Mój klawisz Ctrl jest zepsuty\n",
      "English: My Ctrl key is broken\n",
      "Korean: 제 Ctrl 키가 고장 났어요 \n",
      "\n",
      "Original message (This is Chinese.): 我的屏幕在闪烁\n",
      "English: My screen is flickering\n",
      "Korean: 내 화면이 깜박거립니다 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "for issue in user_messages:\n",
    "    prompt = f\"Tell me what language this is: ```{issue}```\"\n",
    "    lang = get_completion(prompt)\n",
    "    print(f\"Original message ({lang}): {issue}\")\n",
    "\n",
    "    prompt = f\"\"\"\n",
    "    Translate the following  text to English \\\n",
    "    and Korean: ```{issue}```\n",
    "    \"\"\"\n",
    "    response = get_completion(prompt)\n",
    "    print(response, \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e660eb-324f-474c-acf3-7e3bf5b7c70e",
   "metadata": {},
   "source": [
    "## Try it yourself!\n",
    "Try some translations on your own!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fa57158f-d77d-42d1-94fe-17fa59c012f8",
   "metadata": {
    "height": 199
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original message (French): La performance du système est plus lente que d'habitude.\n",
      "English: \"The system performance is slower than usual.\"\n",
      "\n",
      "Korean: \"시스템 성능이 평소보다 느립니다.\" \n",
      "\n",
      "Original message (Spanish): Mi monitor tiene píxeles que no se iluminan.\n",
      "English: \"My monitor has pixels that do not light up.\"\n",
      "\n",
      "Korean: \"내 모니터에는 빛나지 않는 픽셀이 있습니다.\" \n",
      "\n",
      "Original message (Italian): Il mio mouse non funziona\n",
      "English: My mouse is not working\n",
      "Korean: 내 마우스가 작동하지 않습니다 \n",
      "\n",
      "Original message (Polish): Mój klawisz Ctrl jest zepsuty\n",
      "English: My Ctrl key is broken\n",
      "Korean: 제 Ctrl 키가 고장 났어요 \n",
      "\n",
      "Original message (Chinese): 我的屏幕在闪烁\n",
      "English: My screen is flickering\n",
      "Korean: 내 화면이 깜박거립니다 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "for issue in user_messages:\n",
    "    prompt = f\"Tell me what language this is with one word: ```{issue}```\"\n",
    "    lang = get_completion(prompt)\n",
    "    print(f\"Original message ({lang}): {issue}\")\n",
    "\n",
    "    prompt = f\"\"\"\n",
    "    Translate the following  text to English \\\n",
    "    and Korean: ```{issue}```\n",
    "    \"\"\"\n",
    "    response = get_completion(prompt)\n",
    "    print(response, \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d9e54ca-f93a-43c8-a295-bff7a89f77f5",
   "metadata": {
    "id": "JH3-0vdjsILh"
   },
   "source": [
    "## Tone Transformation\n",
    "Writing can vary based on the intended audience. ChatGPT can produce different tones.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2deac328",
   "metadata": {
    "height": 114,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dear Sir/Madam,\n",
      "\n",
      "I am writing to bring to your attention the specifications of the standing lamp. \n",
      "\n",
      "Sincerely,\n",
      "Joe\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Translate the following from slang to a business letter: \n",
    "'Dude, This is Joe, check out this spec on this standing lamp.'\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2c7eb73-6b82-442d-b4f8-251c308e89d5",
   "metadata": {
    "id": "p3e9sZh5tWIa"
   },
   "source": [
    "## Format Conversion\n",
    "ChatGPT can translate between formats. The prompt should describe the input and output formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1438447f",
   "metadata": {
    "height": 199,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nTranslate the following python dictionary from JSON to an HTML table with column headers and title: {'resturant employees': [{'name': 'Shyam', 'email': 'shyamjaiswal@gmail.com'}, {'name': 'Bob', 'email': 'bob32@gmail.com'}, {'name': 'Jai', 'email': 'jai87@gmail.com'}]}\\n\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_json = { \"resturant employees\" :[ \n",
    "    {\"name\":\"Shyam\", \"email\":\"shyamjaiswal@gmail.com\"},\n",
    "    {\"name\":\"Bob\", \"email\":\"bob32@gmail.com\"},\n",
    "    {\"name\":\"Jai\", \"email\":\"jai87@gmail.com\"}\n",
    "]}\n",
    "\n",
    "prompt = f\"\"\"\n",
    "Translate the following python dictionary from JSON to an HTML \\\n",
    "table with column headers and title: {data_json}\n",
    "\"\"\"\n",
    "prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5a37f0a0",
   "metadata": {
    "height": 46,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      "<head>\n",
      "  <title>Restaurant Employees</title>\n",
      "</head>\n",
      "<body>\n",
      "  <table>\n",
      "    <tr>\n",
      "      <th>Name</th>\n",
      "      <th>Email</th>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>Shyam</td>\n",
      "      <td>shyamjaiswal@gmail.com</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>Bob</td>\n",
      "      <td>bob32@gmail.com</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>Jai</td>\n",
      "      <td>jai87@gmail.com</td>\n",
      "    </tr>\n",
      "  </table>\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "481a46b7",
   "metadata": {
    "height": 46,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\n",
       "<head>\n",
       "  <title>Restaurant Employees</title>\n",
       "</head>\n",
       "<body>\n",
       "  <table>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Email</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>Shyam</td>\n",
       "      <td>shyamjaiswal@gmail.com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>Bob</td>\n",
       "      <td>bob32@gmail.com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>Jai</td>\n",
       "      <td>jai87@gmail.com</td>\n",
       "    </tr>\n",
       "  </table>\n",
       "</body>\n",
       "</html>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display, Markdown, Latex, HTML, JSON\n",
    "display(HTML(response))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2df1824c-534b-45cb-b0c1-3000bba5adbe",
   "metadata": {
    "id": "qLTz16qEzyT_"
   },
   "source": [
    "## Spellcheck/Grammar check.\n",
    "\n",
    "Here are some examples of common grammar and spelling problems and the LLM's response. \n",
    "\n",
    "To signal to the LLM that you want it to proofread your text, you instruct the model to 'proofread' or 'proofread and correct'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "52d77283",
   "metadata": {
    "height": 301,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The girl with the black and white puppies has a ball.\n",
      "No errors found\n",
      "No errors found.\n",
      "Their goes my freedom. There going to bring they’re suitcases.\n",
      "\n",
      "No errors found.\n",
      "\n",
      "Rewritten:\n",
      "Their goes my freedom. There going to bring their suitcases.\n",
      "You're going to need your notebook.\n",
      "No errors found.\n",
      "No errors found\n"
     ]
    }
   ],
   "source": [
    "text = [ \n",
    "  \"The girl with the black and white puppies have a ball.\",  # The girl has a ball.\n",
    "  \"Yolanda has her notebook.\", # ok\n",
    "  \"Its going to be a long day. Does the car need it’s oil changed?\",  # Homonyms\n",
    "  \"Their goes my freedom. There going to bring they’re suitcases.\",  # Homonyms\n",
    "  \"Your going to need you’re notebook.\",  # Homonyms\n",
    "  \"That medicine effects my ability to sleep. Have you heard of the butterfly affect?\", # Homonyms\n",
    "  \"This phrase is to cherck chatGPT for speling abilitty\"  # spelling\n",
    "]\n",
    "for t in text:\n",
    "    prompt = f\"\"\"Proofread and correct the following text\n",
    "    and rewrite the corrected version. If you don't find\n",
    "    and errors, just say \"No errors found\". Don't use \n",
    "    any punctuation around the text:\n",
    "    ```{t}```\"\"\"\n",
    "    response = get_completion(prompt)\n",
    "    print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7543fe7d",
   "metadata": {
    "height": 233,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got this for my daughter for her birthday because she keeps taking mine from my room. Yes, adults also like pandas too. She takes it everywhere with her, and it's super soft and cute. One of the ears is a bit lower than the other, and I don't think that was designed to be asymmetrical. It's a bit small for what I paid for it though. I think there might be other options that are bigger for the same price. It arrived a day earlier than expected, so I got to play with it myself before I gave it to my daughter.\n"
     ]
    }
   ],
   "source": [
    "text = f\"\"\"\n",
    "Got this for my daughter for her birthday cuz she keeps taking \\\n",
    "mine from my room.  Yes, adults also like pandas too.  She takes \\\n",
    "it everywhere with her, and it's super soft and cute.  One of the \\\n",
    "ears is a bit lower than the other, and I don't think that was \\\n",
    "designed to be asymmetrical. It's a bit small for what I paid for it \\\n",
    "though. I think there might be other options that are bigger for \\\n",
    "the same price.  It arrived a day earlier than expected, so I got \\\n",
    "to play with it myself before I gave it to my daughter.\n",
    "\"\"\"\n",
    "prompt = f\"proofread and correct this review: ```{text}```\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "11ac80a0",
   "metadata": {
    "height": 80,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "Got this for my daughter for her birthday <span style=\"color:red;font-weight:700;text-decoration:line-through;\">cuz </span><span style=\"color:red;font-weight:700;\">because </span>she keeps taking mine from my <span style=\"color:red;font-weight:700;text-decoration:line-through;\">room.  </span><span style=\"color:red;font-weight:700;\">room. </span>Yes, adults also like pandas <span style=\"color:red;font-weight:700;text-decoration:line-through;\">too.  </span><span style=\"color:red;font-weight:700;\">too. </span>She takes it everywhere with her, and it's super soft and <span style=\"color:red;font-weight:700;text-decoration:line-through;\">cute.  </span><span style=\"color:red;font-weight:700;\">cute. </span>One of the ears is a bit lower than the other, and I don't think that was designed to be asymmetrical. It's a bit small for what I paid for it though. I think there might be other options that are bigger for the same <span style=\"color:red;font-weight:700;text-decoration:line-through;\">price.  </span><span style=\"color:red;font-weight:700;\">price. </span>It arrived a day earlier than expected, so I got to play with it myself before I gave it to my <span style=\"color:red;font-weight:700;text-decoration:line-through;\">daughter.\n",
       "</span><span style=\"color:red;font-weight:700;\">daughter.</span>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from redlines import Redlines\n",
    "\n",
    "diff = Redlines(text,response)\n",
    "display(Markdown(diff.output_markdown))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2b4e73fd",
   "metadata": {
    "height": 148,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "I purchased this adorable panda plush toy for my daughter's birthday as she kept borrowing mine from my room. It's not just for kids, adults can appreciate the cuteness of pandas too. The plush is incredibly soft and my daughter carries it everywhere with her. However, I did notice that one of the ears is slightly lower than the other, which seems like a design flaw rather than intentional asymmetry. Despite this, I found the size to be a bit smaller than expected given the price. I believe there are larger options available for the same cost. On a positive note, the item arrived a day earlier than anticipated, allowing me to enjoy playing with it before gifting it to my daughter. Overall, while there are some minor issues, the quality and early delivery make this panda plush a worthwhile purchase for any panda enthusiast."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "proofread and correct this review. Make it more compelling. \n",
    "Ensure it follows APA style guide and targets an advanced reader. \n",
    "Output in markdown format.\n",
    "Text: ```{text}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "display(Markdown(response))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63fb76bc-a742-4b35-9dc2-f7fbc12d38fb",
   "metadata": {},
   "source": [
    "## Try it yourself!\n",
    "Try changing the instructions to form your own review."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2b2ca58",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3dbf5020-7d7f-4ba5-840b-20e883cd7c99",
   "metadata": {
    "id": "unsf1JnRr2IC"
   },
   "source": [
    "Thanks to the following sites:\n",
    "\n",
    "https://writingprompts.com/bad-grammar-examples/\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
